﻿using IDEAMS.core;
using IDEAMS.core.deviceClient;
using IDEAMS.core.opm;
using IDEAMS.model;
using IDEAMS.utils;
using System;
using System.Windows;


namespace IDEAMS.view.setting
{
    /// <summary>
    /// EditDeviceIpDlg.xaml 的交互逻辑
    /// </summary>
    public partial class EditDeviceIpDlg : Window
    {
        public EditDeviceIpDlgViewModel windowViewModel;

        private string oldDeviceIp;
        private DeviceInfo deviceInfo;

        private ReadDeviceBaseInfoResp resp = null;

        public EditDeviceIpDlg()
        {
            InitializeComponent();
            windowViewModel = new EditDeviceIpDlgViewModel();
            this.DataContext = windowViewModel;
        }

        public EditDeviceIpDlg(DeviceInfoViewModel deviceInfo) : this()
        {
            oldDeviceIp = deviceInfo.deviceParam.ip;
            this.deviceInfo = new DeviceInfo(deviceInfo);
            windowViewModel.deviceIp = deviceInfo.deviceParam.ip;
            this.Loaded += Window_Loaded;

            this.btnOK.Click += new RoutedEventHandler(OK_Click);
            this.btnCancel.Click += new RoutedEventHandler(Cancel_Click);
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            if(oldDeviceIp != windowViewModel.deviceIp)
            {
                SetDeviceParams();
            }
            this.DialogResult = true;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ReadDeviceParams();
        }

        public void ReadDeviceParams()
        {
            DeviceClient client = TW_App.deviceClientManager.Find(deviceInfo.serialNo);
            if (client == null) return;

            resp = client.dataWorker.GetDeviceParameter();
            if(resp != null)
            {
                windowViewModel.deviceIp = DataConverter.ipAddressToString(resp.deviceIP);
            }
            else
            {
                MessageBox.Show(this, "获取设备信息失败，请确认设备连接正常并且未处于烤机状态", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }


        public void SetDeviceParams()
        {
            DeviceClient client = TW_App.deviceClientManager.Find(deviceInfo.serialNo);
            if (client == null) return;

            if(resp != null)
            {
                resp.deviceIP = DataConverter.stringToIPAddress(windowViewModel.deviceIp);
                int result = client.dataWorker.SetDeviceParameter(resp);
                if(result < 0)
                {
                    MessageBox.Show(this, "修改设备IP失败，请确认设备连接正常并且未处于烤机状态", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    MessageBox.Show(this, "修改成功,请重启设备", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            else
            {
                MessageBox.Show(this, "获取设备信息失败，请确认设备连接正常并且未处于烤机状态", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }

    }

    public class EditDeviceIpDlgViewModel : ViewModelBase
    {
        public EditDeviceIpDlgViewModel()
        {

        }

        private string m_deviceIp;
        public string deviceIp
        {
            get { return m_deviceIp; }
            set
            {
                if (m_deviceIp != value)
                {
                    m_deviceIp = value;
                    NotifyPropertyChanged();
                }
            }
        }
    }
}
